package com.photons.artp.rpc

import com.photons.artp.CameraStreamConsumer
import com.photons.artp.rpc.CameraStreamServiceGrpc.*
import com.photons.jrtplib.JRTP
import com.photons.jrtplib.RtpListener
import io.grpc.stub.StreamObserver

class CameraRPCService : CameraStreamServiceImplBase(), RtpListener {
    private var mRTPHandle = 0L

    private val mCameraStreamConsumer = object : CameraStreamConsumer {
        override fun onFrameReady(data: ByteArray) {

        }

    }

    fun getCameraStreamConsumer() : CameraStreamConsumer {
        return mCameraStreamConsumer
    }

    override fun open(
        request: CameraStreamRequest?,
        responseObserver: StreamObserver<CameraStreamReply>?
    ) {
        if (mRTPHandle == 0L) {
            mRTPHandle = JRTP.initSender(1234,
                request?.session?.ip ?: "localhost",
                request?.session?.port ?: 1234, this)
        }



        val ret = if (mRTPHandle <= 0L) {
            ResultCode.FAIL
        } else {
            ResultCode.OK
        }


    }

    override fun close(request: RTPSession?, responseObserver: StreamObserver<CameraStreamReply>?) {
        super.close(request, responseObserver)
    }

    override fun receiveRtpPacket(
        data: ByteArray?,
        pkgSize: Int,
        hasMaker: Boolean,
        lastTime: Long
    ) {
    }

    override fun receiveRtcpData(remoteIP: String?) {
    }

    override fun receiveBye(remoteIP: String?) {
    }
}